<?php
/*
 * 数组文件处理类
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CArray.lib.php 2010-10-06 23:12:10Z andery $
 */
class CArrayFile 
{
    private $_filename;
    private $_data;
    
    public function __construct($filename)
    {
        $this->_filename = $filename;
        $this->_data = is_file($this->_filename) ? include($this->_filename) : array();
    }
    
    public function get_all()
    {
        return $this->_data;
    }
    
    public function get($key)
    {
        return isset($this->_data[$key]) ? $this->_data[$key] : NULL;
    }
    
    public function set($data)
    {
        if (!$data) {
            return false;
        }
        foreach ($data as $key => $value) {
            isset($value) && $this->_data[$key] = $value;
        }
        return $this->_save();
    }
    
    public function dorp($key)
    {
        unset($this->_data[$key]);
        return $this->_save();
    }
    
    private function _save()
    {
        return file_put_contents($this->_filename, "<?php \nreturn " . var_export($this->_data, true) . ";\n?>", LOCK_EX);
    }
}